<?php

/**
 * Created by PhpStorm.
 * User: cyj
 * Date: 15-7-24
 * Time: 上午10:27
 */
class AdminCultureController extends AdminController {

    use \DavinBao\Workflow\HasFlowForResourceController;

    protected $culture;
    protected $entryName = 'cultures';

    public function __construct(Culture $culture) {
        $this->culture = $culture;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getIndex() {
        // Title
        $title = Lang::get('admin/cultures/title.management');
        $cultures = array();
        switch (Input::get('s')) {
            case 'audit':
                $cultures = Culture::getAuditList()->paginate(Config::get('app.pagenate_num'));
                break;
            case 'completed':
                $cultures = Culture::getCompletedList()->paginate(Config::get('app.pagenate_num'));
                break;
            default:
                $cultures = Culture::orderBy('updated_at', 'DESC')->paginate(Config::get('app.pagenate_num'));
                break;
        }

        // Show the page
        return View::make(Config::get('app.admin_template') . '/cultures/index', compact('cultures', 'title'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function getCreate() {
        // Title
        $title = Lang::get('admin/cultures/title.create');
        $entryName = $this->entryName;
        // Mode
        $mode = 'create';
        // Show the page
        return View::make(Config::get('app.admin_template') . '/cultures/create_edit', compact('title', 'entryName', 'mode'));
    }

    public function postCreate() {
        $image = Input::get('image');
        $imagearry = array('culture1.jpg', 'culture2.jpg');
        if ($image == "") {
            //$a = array_rand($imagearry, 1);
            //$image = $imagearry[$a];
        }
        $this->culture->    culture_name = Input::get('culture_name');
        $this->culture->image = $image;
        $this->culture->culture_content = Input::get('culture_content');
        $this->culture->freeze = Input::get('freeze');

        // before saving. This field will be used in Ardent's
        // auto validation.
        $this->culture->freeze = Input::get('freeze');

        if ($this->culture->save(Culture::$rules)) {
            // Redirect to the new user page
            return Redirect::to('admin/cultures/' . $this->culture->id . '/binding');
        } else {
            // Get validation errors (see Ardent package)
            $error = $this->culture->errors()->all();

            return Redirect::to('admin/cultures/create')
                            ->with('error', $error);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @return Response
     */
    public function getEdit($entry) {
        if ($entry->id) {
            // Title
            //$title = Lang::get('admin/cultures/title.update');
            // mode
            $mode = 'edit';
            $entryName = $this->entryName;

            return \View::make(\Config::get('app.admin_template') . '/' . $this->entryName . '/create_edit', compact('entry', 'entryName', 'mode'));
        } else {
            return \Redirect::to('admin/culture')->with('error', \Lang::get('admin/' . $this->entryName . '/messages.does_not_exist'));
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param $culture
     * @return Response
     */
    public function postEdit($culture) {
        $image = Input::get('image');
        // Validate the inputs
        $validator = Validator::make(Input::all(), Culture::$rules);
        $imagearry = array('culture1.jpg', 'culture2.jpg');
        if ($image == "") {
            //$a = array_rand($imagearry, 1);
            //$image = $imagearry[$a];
        }
        // Check if the form validates with success
        if ($validator->passes()) {

            $culture->culture_name = Input::get('culture_name');
            $culture->image = $image;
            $culture->culture_content = Input::get('culture_content');
            $culture->freeze = Input::get('freeze');
            // Was the role updated?
            if ($culture->save()) {
                // Redirect to the role page
                return Redirect::to('admin/cultures/' . $culture->id . '/binding');
            } else {
                // Redirect to the role page
                return Redirect::to('admin/cultures/' . $culture->id . '/edit')->with('error', Lang::get('admin/cultures/messages.update.error'));
            }
        }

        // Form validation failed
        return Redirect::to('admin/cultures/' . $culture->id . '/edit')->withInput()->withErrors($validator);
    }

    /**
     * Remove the specified user from storage.
     *
     * @param $culture
     * @internal param $id
     * @return Response
     */
    public function postDelete($culture) {
        // Was the role deleted?
        if ($culture->delete()) {
            // Redirect to the role management page
            return Redirect::to('admin/cultures')->with('success', Lang::get('admin/cultures/messages.delete.success'));
        }

        // There was a problem deleting the role
        return Redirect::to('admin/cultures')->with('error', Lang::get('admin/cultures/messages.delete.error'));
    }

    public function postPreview() {
        $culture = new Culture();
        $data = Input::all();
        $culture->culture_name = $data['culture_name'];
        $culture->image = $data['image'];
        $culture->culture_content = $data['culture_content'];
        $culture->freeze = $data['freeze'];
        $culture->created_at = date("Y-m-d");
        $culture->updated_at = date("Y-m-d");

        $services = Business::where('freeze', '=', '0')->orderBy('order', 'DESC')->take(4)->get();
        $setting = Setting::find(1);
        $cultures = Culture::orderBy('updated_at', 'DESC')->paginate(5);

        return View::make(Config::get('app.front_template') . '/cultures_show', compact('services', 'setting', 'cultures', 'culture'));
    }

}
